package com.health_predict_springboot.controller;

import com.health_predict_springboot.pojo.SysPopupConfig;
import com.health_predict_springboot.service.SysPopupConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/popup-config")
public class SysPopupConfigController {

    @Autowired
    private SysPopupConfigService sysPopupConfigService;

    @GetMapping("/{id}")
    public SysPopupConfig getPopupConfigById(@PathVariable("id") Integer id) {
        return sysPopupConfigService.getPopupConfigById(id);
    }

    @GetMapping("/all")
    public List<SysPopupConfig> getAllPopupConfigs() {
        return sysPopupConfigService.getAllPopupConfigs();
    }

    @PostMapping
    public boolean addPopupConfig(@RequestBody SysPopupConfig popupConfig) {
        Date now = new Date();
        popupConfig.setCreateTime(now);
        popupConfig.setUpdateTime(now);
        return sysPopupConfigService.addPopupConfig(popupConfig);
    }

    @PutMapping
    public boolean updatePopupConfig(@RequestBody SysPopupConfig popupConfig) {
        popupConfig.setUpdateTime(new Date());
        return sysPopupConfigService.updatePopupConfig(popupConfig);
    }

    @DeleteMapping("/{id}")
    public boolean deletePopupConfig(@PathVariable("id") Integer id) {
        return sysPopupConfigService.deletePopupConfig(id);
    }

    @GetMapping("/page")
    public List<SysPopupConfig> getPopupConfigsByPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String popupType,
            @RequestParam(required = false) Integer isActive) {
        Map<String, Object> params = Map.of(
                "popupType", popupType,
                "isActive", isActive,
                "offset", (pageNum - 1) * pageSize,
                "pageSize", pageSize
        );
        return sysPopupConfigService.getPopupConfigsByPage(params);
    }

    @GetMapping("/count")
    public int getPopupConfigCount(
            @RequestParam(required = false) String popupType,
            @RequestParam(required = false) Integer isActive) {
        Map<String, Object> params = Map.of(
                "popupType", popupType,
                "isActive", isActive
        );
        return sysPopupConfigService.getPopupConfigCount(params);
    }

    @GetMapping("/active")
    public SysPopupConfig getActivePopupConfig() {
        return sysPopupConfigService.getActivePopupConfig();
    }
}